//如果想要发送异步请求，我们需要一个关键的对象 XMLHttpRequest
var xmlHttpRequest ;

function createXMLHttpRequest(){
    if(window.XMLHttpRequest){
        //符合DOM2标准的浏览器 ，xmlHttpRequest的创建方式
        xmlHttpRequest = new XMLHttpRequest() ;
    }else if(window.ActiveXObject){//IE浏览器
        try{
            xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }catch (e) {
            xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP")
        }
    }
}

const second = 60;
let delay = second;
let timer = null;
let btn = null;

function fn(){
    if(delay != 0){
        btn.innerText = delay+'s';
        btn.style.backgroundColor = "#999"
        delay--;
    }else {
        clearInterval(timer);
        btn.innerText = "重新获取";
        btn.style.backgroundColor = "orange";
        delay = second;
        btn.disabled = false
    }
    return fn
}

function getCode(){
    createXMLHttpRequest();
    var telephone;
    var form=document.getElementById("register_data");
    var formData=new FormData(form);
    telephone = formData.get("telephone");

    btn = document.querySelector("button");
    // btn.onclick=function (){
        btn.disabled = true;
        timer = setInterval(fn(),1000);
    // }

    var url = "getCodeNum/"+telephone;
    xmlHttpRequest.open("GET",url,true);
    //设置回调函数  当状态有改变的时候，就去调用回调函数（chUnameCB）
    xmlHttpRequest.onreadystatechange = getCodeAjax ;
    //发送请求
    xmlHttpRequest.send();
}


function getCodeAjax(){

    if(xmlHttpRequest.readyState==4 && xmlHttpRequest.status==200){
        //xmlHttpRequest.responseText 表示 服务器端响应给我的文本内容
        //alert(xmlHttpRequest.responseText);
        var responseText = xmlHttpRequest.responseText ;// {'uname':'1'}
        // alert(responseText);
    }
}






// ====================表单验证========================
function $(id){
    return document.getElementById(id);
}

function preRegist(){

    //用户名不能为空，而且是6~16位数字和字母组成
    let unameReg = /[0-9a-zA-Z]{6,16}/;
    let unameTxt = $("username");
    let uname = unameTxt.value;
    let unameSpan = $("unameSpan");

    if(!unameReg.test(uname)){
        //不一致的时候，将span的提示信息显示到页面提醒用户
        unameSpan.style.visibility="visible";
        return false ;
    }else{
        unameSpan.style.visibility="hidden";
    }

    let accountEle = $("accountNo");
    let accountNo = accountEle.value ;
    let pwdReg1 = /[\w]{6,}/; // /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/;
    let accountSpan = $("account");
    if(!pwdReg1.test(accountNo)){
        accountSpan.style.visibility="visible";
        return false ;
    }else{
        accountSpan.style.visibility="hidden";
    }

    //密码的长度至少为8位
    let pwdTxt = $("firstPassword");
    let pwd = pwdTxt.value ;
    let pwdReg = /[\w]{8,}/; // /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/;
    let pwdSpan = $("pwdSpan");
    if(!pwdReg.test(pwd)){
        pwdSpan.style.visibility="visible";
        return false ;
    }else{
        pwdSpan.style.visibility="hidden";
    }

    //密码两次输入不一致
    let pwd2 = $("secondPassword").value ;
    let pwdSpan2 = $("pwdSpan2") ;
    if(pwd2!=pwd){
        pwdSpan2.style.visibility="visible";
        return false ;
    }else{
        pwdSpan2.style.visibility="hidden";
    }

    //请输入正确的邮箱格式
    let email = $("emailTxt").value ;
    let emailSpan = $("emailSpan");
    let emailReg = /^[a-zA-Z0-9_\.-]+@([a-zA-Z0-9-]+[\.]{1})+[a-zA-Z]+$/;
    if(!emailReg.test(email)){
        emailSpan.style.visibility="visible";
        return false ;
    }else{
        emailSpan.style.visibility="hidden";
    }


    // /^1[3456789]\d{9}$/
    let telephone = $("telephone").value;
    let phoneSpan = $("phone");
    let phoneReg = /^1[3456789]\d{9}$/;
    if(!phoneReg.test(telephone)){
        phoneSpan.style.visibility="visible";
        return false ;
    }else{
        phoneSpan.style.visibility="hidden";
    }

    return true ;
}



